/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.entity;

import entity.Courses;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import jpa.controllers.CoursesJpaController;

/**
 *
 * @author atap
 */
public class CoursesConverter implements Converter {
     @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        Integer id = new Integer(value);
        CoursesJpaController controller = (CoursesJpaController) context.getApplication().getELResolver().getValue(context.getELContext(), null, "coursesJpa");
        return controller.findCourses(id);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Courses) {
            Courses o = (Courses) value;
            return o.getCourseId() == null ? "" : o.getCourseId().toString();
        } else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: entity.Courses");
        }
    }
}
